package com.hnevc.vx;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import com.hnevc.vx.Fragment.Fragment1;
import com.hnevc.vx.Fragment.Fragment2;
import com.hnevc.vx.Fragment.Fragment3;
import com.hnevc.vx.Fragment.Fragment4;
import com.hnevc.vx.adapter.MyAdapter;
import java.util.ArrayList;

public class ShouyeActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private LinearLayout shezhe;
    private LinearLayout lt1;
    private LinearLayout lt2;
    private LinearLayout lt3;
    private LinearLayout lt4;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewpager);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        shezhe = findViewById(R.id.shezhi);
        viewPager = findViewById(R.id.viewpager);
        lt1 = findViewById(R.id.fragment1);
        lt2 = findViewById(R.id.fragment2);
        lt3 = findViewById(R.id.fragment3);
        lt4 = findViewById(R.id.fragment4);
        Fragment1 fragment1 = new Fragment1();
        Fragment2 fragment2 = new Fragment2();
        Fragment3 fragment3 = new Fragment3();
        Fragment4 fragment4 = new Fragment4();
        ArrayList arrayList = new ArrayList();
        arrayList.add(fragment1);
        arrayList.add(fragment2);
        arrayList.add(fragment3);
        arrayList.add(fragment4);
        viewPager.setAdapter(new MyAdapter(getSupportFragmentManager(), ShouyeActivity.this, arrayList));
        viewPager.setCurrentItem(0);   //默认打开的fragment
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        lt1.setBackgroundColor(Color.parseColor("#DBD4D4"));
                        lt2.setBackgroundColor(Color.parseColor("#FFFFFF"));
                        lt3.setBackgroundColor(Color.parseColor("#FFFFFF"));
                        lt4.setBackgroundColor(Color.parseColor("#FFFFFF"));
                        break;
                    case 1:
                        lt1.setBackgroundColor(Color.parseColor("#FFFFFF"));
                        lt2.setBackgroundColor(Color.parseColor("#DBD4D4"));
                        lt3.setBackgroundColor(Color.parseColor("#FFFFFF"));
                        lt4.setBackgroundColor(Color.parseColor("#FFFFFF"));
                        break;
                    case 2:
                        lt1.setBackgroundColor(Color.parseColor("#FFFFFF"));
                        lt2.setBackgroundColor(Color.parseColor("#FFFFFF"));
                        lt3.setBackgroundColor(Color.parseColor("#DBD4D4"));
                        lt4.setBackgroundColor(Color.parseColor("#FFFFFF"));
                        break;
                    case 3:
                        lt1.setBackgroundColor(Color.parseColor("#FFFFFF"));
                        lt2.setBackgroundColor(Color.parseColor("#FFFFFF"));
                        lt3.setBackgroundColor(Color.parseColor("#FFFFFF"));
                        lt4.setBackgroundColor(Color.parseColor("#DBD4D4"));
                        break;
                }
            }
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initData() {

    }

    private void initEvent() {
        lt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(0);
            }
        });
        lt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(1);
            }
        });
        lt3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(2);
            }
        });
        lt4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(3);
            }
        });
    }

}
